package com.runen.infinitemovement.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.CompanyGroupAdapter;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.model.entity.HomeEntity;
import com.runen.infinitemovement.model.entity.NextAllCompanyStructureEntity;
import com.runen.infinitemovement.model.entity.TeamStructureEntity;
import com.runen.infinitemovement.tool.GlideUtils;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.JsonUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TeamGroupActivity extends BaseActivity {

    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.tv_company_name)
    TextView tvCompanyName;
    @BindView(R.id.tv_principal)
    TextView tvPrincipal;
    @BindView(R.id.rv_team_group_list)
    RecyclerView rvTeamGroupList;
    private CompanyGroupAdapter companyGroupAdapter;
    private HomeEntity.DataBean.TopCompanyInfoBean topCompanyInfoBean;
    private String companyId;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_team_group;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        topCompanyInfoBean = JsonUtils.fromJson(getIntent().getStringExtra("CompanyInfo"), HomeEntity.DataBean.TopCompanyInfoBean.class);
        if (topCompanyInfoBean != null) {
            companyId = topCompanyInfoBean.companyId;
            tvCompanyName.setText(topCompanyInfoBean.companyName);
            String str = "负责人:%s";
            tvPrincipal.setText(String.format(str, topCompanyInfoBean.leader));
        }
        topTitle.setText("团队架构");
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvTeamGroupList.setLayoutManager(layoutManager);
        companyGroupAdapter = new CompanyGroupAdapter(this);
        rvTeamGroupList.setAdapter(companyGroupAdapter);

        iniData();
        companyGroupAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (null != companyGroupAdapter.getData().get(position).departments && companyGroupAdapter.getData().get(position).departments.size() > 0) {
                    Intent intent = new Intent(TeamGroupActivity.this, NxetTeamGroupActivity.class);
                    intent.putExtra("NEXT_TEAM_INFO", JsonUtils.toJson(companyGroupAdapter.getData().get(position)));
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(TeamGroupActivity.this, LastTeamGroupActivity.class);
                    intent.putExtra("NEXT_TEAM_INFO", JsonUtils.toJson(companyGroupAdapter.getData().get(position)));
                    startActivity(intent);
                }
            }
        });
    }

    private void iniData() {
        String string = "?companyId=%s";
        new UserAPI().getNextAllCompany2(String.format(string, companyId), new BaseResultCallback<TeamStructureEntity>() {
            @Override
            public void onResponse(TeamStructureEntity response) {
                if (response.code == 0) {
                    if (response.data != null) {
                        companyGroupAdapter.setNewData(response.data);
                    }
                }
            }
        });
    }

    @OnClick(R.id.top_btn_left)
    public void onViewClicked() {
        finish();
    }
}
